package com.fancy.learncenter.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.AllCityDataBean;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.MySchoolDetailDataBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.ui.adapter.base.CustomViewHold;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class SelectCityActivity extends BaseActivity {

    @Bind(R.id.recyclerview_pro)
    RecyclerView recyclerviewPro;
    @Bind(R.id.recyclerview_city)
    RecyclerView recyclerviewCity;
    @Bind(R.id.recyclerview_hot)
    RecyclerView recyclerviewHot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_city, BaseActivity.Status.STATUS, "选择学校");
        ButterKnife.bind(this);
        initHotView();
        initProAndCityView();

        getAllCity();
    }

    ArrayList<AllCityDataBean.HotCityListBean> hotDatas;
    ArrayList<AllCityDataBean.AllCityListBean> proDatas;
    ArrayList<AllCityDataBean.AllCityListBean.ChildAreaListBean> cityDatas;
    ProAdapter proAdapter;
    HotAdapter hotAdapter;
    CityAdapter cityAdapter;

    private void initHotView() {
        hotDatas = new ArrayList<>();
        GridLayoutManager manager = new GridLayoutManager(this, 4);
        recyclerviewHot.setLayoutManager(manager);
        hotAdapter = new HotAdapter(this, hotDatas);
        recyclerviewHot.setAdapter(hotAdapter);
    }

    private void initProAndCityView() {
        proDatas = new ArrayList<>();
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerviewPro.setLayoutManager(manager);

        proAdapter = new ProAdapter(this, proDatas);
        recyclerviewPro.setAdapter(proAdapter);


        cityDatas = new ArrayList<>();
        LinearLayoutManager manager2 = new LinearLayoutManager(this);
        manager2.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerviewCity.setLayoutManager(manager2);
        cityAdapter = new CityAdapter(this, cityDatas);
        recyclerviewCity.setAdapter(cityAdapter);
    }

    class HotAdapter extends CommonRecycleViewAdapter<AllCityDataBean.HotCityListBean> {

        public HotAdapter(Context mContext, List<AllCityDataBean.HotCityListBean> itemDatas) {
            super(mContext, R.layout.textview_item, itemDatas);
        }

        @Override
        public void bindView(CustomViewHold customViewHold, final AllCityDataBean.HotCityListBean item, int position) {
            TextView textView = (TextView) customViewHold.getRoorView();
            textView.setText(item.getCityName());

            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(SelectCityActivity.this, BindSchoolActivity.class);
                    intent.putExtra("name", item.getCityName());
                    intent.putExtra("id", item.getCityId());
                    startActivity(intent);

                }
            });
        }
    }

    class ProAdapter extends CommonRecycleViewAdapter<AllCityDataBean.AllCityListBean> {

        public ProAdapter(Context mContext, List<AllCityDataBean.AllCityListBean> itemDatas) {
            super(mContext, R.layout.textview_item, itemDatas);
        }

        @Override
        public void bindView(CustomViewHold customViewHold, final AllCityDataBean.AllCityListBean item, final int position) {
            TextView proName = (TextView) customViewHold.getRoorView();
            proName.setText(item.getName());
            if (item.isSelected()) {
                proName.setBackgroundColor(Color.parseColor("#FFFFFF"));
                proName.setTextColor(getResources().getColor(R.color.yello));
            } else {
                proName.setBackgroundColor(Color.parseColor("#FAFAFA"));
                proName.setTextColor(getResources().getColor(R.color.bind_text_color));
            }

            proName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cityDatas.removeAll(cityDatas);
                    cityDatas.addAll(item.getChildAreaList());
                    cityAdapter.notifyDataSetChanged();

                    for (int i = 0; i < proDatas.size(); i++) {
                        if (proDatas.get(i).isSelected()) {
                            proDatas.get(i).setSelected(false);

                            notifyItemChanged(i);
                        }
                    }
                    item.setSelected(true);
                    notifyItemChanged(position);
                }
            });

        }
    }

    class CityAdapter extends CommonRecycleViewAdapter<AllCityDataBean.AllCityListBean.ChildAreaListBean> {

        public CityAdapter(Context mContext, List<AllCityDataBean.AllCityListBean.ChildAreaListBean> itemDatas) {
            super(mContext, R.layout.textview_item, itemDatas);
        }

        @Override
        public void bindView(CustomViewHold customViewHold, final AllCityDataBean.AllCityListBean.ChildAreaListBean item, int position) {
            TextView cityName = (TextView) customViewHold.getRoorView();
            cityName.setText(item.getName());
            cityName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(SelectCityActivity.this, BindSchoolActivity.class);
                    intent.putExtra("name", item.getName());
                    intent.putExtra("id", item.getId());
                    startActivity(intent);

                }
            });
        }
    }


    private void getAllCity() {
        HttpMehtod.getInstance().getAllCity(new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                //解析数据
                BaseBean<AllCityDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), AllCityDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<AllCityDataBean>() {
                    @Override
                    public void success(AllCityDataBean dataBean) {
                        hotDatas.addAll(dataBean.getHotCityList());
                        dataBean.getAllCityList().get(0).setSelected(true);
                        proDatas.addAll(dataBean.getAllCityList());
                        cityDatas.addAll(dataBean.getAllCityList().get(0).getChildAreaList());
                    }

                    @Override
                    public void fail(String errorCode) {

                    }

                }).dealData();
            }

            @Override
            public void onCompleted() {
                super.onCompleted();

                hotAdapter.notifyDataSetChanged();
                proAdapter.notifyDataSetChanged();
                cityAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
